﻿using System;
using System.Web;

namespace MakeItWeb.Framework
{
    public class CustomErrorModule : IHttpModule
    {
        /// <summary>
        /// You will need to configure this module in the web.config file of your
        /// web and register it with IIS before being able to use it. For more information
        /// see the following link: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpModule Members

        public void Dispose()
        {
            //clean-up code here.
        }

        public void Init(HttpApplication context)
        {
            context.Error += new EventHandler(OnError);
        }

        #endregion

        public void OnError(object sender, EventArgs e)
        {
            // At this point we have information about the error
            HttpContext ctx = HttpContext.Current;

            Exception exception = ctx.Server.GetLastError();
            HttpContext.Current.Items["LastException"] = exception.InnerException == null ? exception : exception.InnerException;

            ctx.Server.ClearError();

            ctx.Server.Transfer("/ErrorPage.aspx");
        }
    }
}
